#include "ftradiobutton.h"
#include "ftcontrols.h"
#include <QPainter>

FTRadioButton::FTRadioButton(QWidget *parent) :
    QAbstractButton(parent)
{
    initialize("");
}

FTRadioButton::FTRadioButton(const QString& txt, QWidget *parent) :
    QAbstractButton(parent)
{
    initialize(txt);
}

void FTRadioButton::initialize(const QString & s)
{
    setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
    setText(s);
    setCheckable(true);
    setAutoExclusive(true);
}

void FTRadioButton::paintEvent(QPaintEvent *)
{
    int padding = FTControls::defaultPadding();
    int check_padding = padding+1;
    QPainter painter(this);
    int check_size = rect().height() - check_padding*2;

    QRect border = rect().adjusted( check_size + check_padding*2 + padding,
                                    padding,
                                    -(padding+1),
                                    -(padding+1) );

    border = painter.boundingRect( border,
                                   Qt::AlignLeft | Qt::AlignVCenter, text() );

    painter.drawEllipse( check_padding,
                      check_padding,
                      check_size,
                      check_size );
    if ( isChecked() )
    {
        painter.drawLine( check_padding,
                          check_padding,
                          check_padding+check_size,
                          check_padding+check_size );
        painter.drawLine( check_padding,
                          check_padding+check_size,
                          check_padding+check_size,
                          check_padding );
    }
    painter.drawText( border,Qt::AlignLeft | Qt::AlignVCenter ,text() );
}

QSize FTRadioButton::sizeHint() const
{
    int padding = FTControls::defaultPadding();
    int check_padding = padding+1;
    int min_height = FTControls::defaultHeight();
    QSize size;
    QFontMetrics fm(font());
    size = fm.size( Qt::TextSingleLine, text() )+QSize( check_padding*2,padding/2 );
    if ( size.width() < min_height )
        size.setWidth( min_height );
    if ( size.height() < min_height )
        size.setHeight( min_height );
    return size;
}


